#ifndef _TEXTURE_HPP
#define _TEXTURE_HPP
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <SOIL.h>

struct texture_util {
	static GLuint 
	loadtexture(const char *filename) {
		int width, height;
	    unsigned char *image = SOIL_load_image(filename, &width, &height, NULL, SOIL_LOAD_RGB);
		if (image == NULL)
			return 0;
		
		GLuint tex = 0;
		glGenTextures(1, &tex);
		glBindTexture(GL_TEXTURE_2D, tex);
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
			glGenerateMipmap(GL_TEXTURE_2D);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glBindTexture(GL_TEXTURE_2D, 0);
		
		SOIL_free_image_data(image);
		return tex;
	}

	// Loads a cubemap texture from 6 individual texture faces, order should be:
	// +X (right)
	// -X (left)
	// +Y (top)
	// -Y (bottom)
	// +Z (back)
	// -Z (front)
	static GLuint 
	loadcubemap(std::vector<std::string> faces) {
	    GLuint tex;
	    glGenTextures(1, &tex);
	    glBindTexture(GL_TEXTURE_CUBE_MAP, tex);

	    int width, height;
	    unsigned char* image;
	    for (GLuint i = 0; i < faces.size(); i++) {
	        image = SOIL_load_image(faces[i].c_str(), &width, &height, 0, SOIL_LOAD_RGB);
	        glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
	    }
	    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	    glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

	    return tex;
	}
};

#endif
